package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.domain.Partner;
import com.lkd.domain.VendingMachine;
import com.lkd.mapper.PartnerMapper;
import com.lkd.pageParam.PartnerPageParam;
import com.lkd.pageParam.SkuPageParam;
import com.lkd.service.IPartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.service.IVendingMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-09
 */
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements IPartnerService {
    @Autowired
    IVendingMachineService vendingMachineService;
    @Override
    public Page<Partner> getPage(PartnerPageParam param) {
        Page<Partner> page = new Page<>();
        page.setPages(param.getPageIndex()).setSize(param.getPageSize());
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getName,param.getName()) ;
         page(page, wrapper);
        page.getRecords().stream().forEach(s->{
            LambdaQueryWrapper<VendingMachine> vendingMachineLambdaQueryWrapper =
                    new LambdaQueryWrapper<>();
            vendingMachineLambdaQueryWrapper.eq(VendingMachine::getOwnerName,s.getName());
            s.setVmCount(vendingMachineService.count(vendingMachineLambdaQueryWrapper));
        });
        return page(page,wrapper);
    }

    @Override
    public Partner searchById(Long id) {
        Partner partner = getById(id);
        LambdaQueryWrapper<VendingMachine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VendingMachine::getOwnerName,partner.getName());
        partner.setVmCount(vendingMachineService.count(wrapper));
        return partner;
    }

    @Override
    public Integer getPatnerCount() {

        return count();
    }


}
